package com.soecode.lyf.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Lists;
import com.soecode.lyf.entity.Dept;
import com.soecode.lyf.entity.PersonRecord;
import com.soecode.lyf.service.DeptService;
import com.soecode.lyf.service.PersonRecordService;
import com.soecode.lyf.utils.ExcelExport;

/**
 * Created by lijie on 16/11/17.
 */
@Controller
@RequestMapping("/personRecord")
public class PersonRecordController {
	Logger logger = LoggerFactory.getLogger(PersonRecordController.class);

	@Autowired
	PersonRecordService personRecordService;
	@Autowired
	DeptService deptService;

	@RequestMapping(value = "/queryPersonRecord", method = RequestMethod.POST)
	public ModelAndView queryPersonRecord(HttpServletRequest request, String name, String dept, String beginDate,
			String endDate) throws ParseException {
		logger.info("parameter name:{},depe:{},beginDate:{},endDate:{}", name, dept, beginDate, endDate);
		if (endDate != null && endDate != "") {
			endDate = endDate + " 23:59:59.000";
		}
		List<PersonRecord> personRecordList = personRecordService.queryPersonRecord(name, dept, beginDate, endDate);
		logger.info("personRecordList size:{}", personRecordList.size());

		List<Dept> deptList = deptService.queryAllDept();

		ModelAndView mad = new ModelAndView("inputInfo/personRecord");
		mad.addObject("personRecordList", personRecordList);
		mad.addObject("deptList", deptList);
		return mad;
	}

	@RequestMapping(value = "/getPersonRecordExcel", method = RequestMethod.POST)
	@ResponseBody
	public void getPersonRecordExcel(HttpServletResponse response, String name, String dept, String beginDate,
			String endDate, int excelType) throws Exception {

		long startTime = System.currentTimeMillis(); // 获取开始时间
		String sheetName = "员工打卡记录";
		int columnNumber = 9;
		String[] columnName = { "IC卡号", "ID卡号", "持卡人编号", "持卡人姓名", "区域", "性别", "身份证号", "门号", "时间" };
		ExcelExport ee = new ExcelExport();

		if (endDate != null && endDate != "") {
			endDate = endDate + " 23:59:59.000";
		}
		HSSFWorkbook wb = null;
		List<PersonRecord> personRecordList = Lists.newArrayList();
		if (excelType == 0) {
			personRecordList = personRecordService.queryPersonRecord(name, dept, beginDate, endDate);
			wb = ee.exportNoResponse(sheetName, columnNumber, columnName, personRecordList);
		} else {
			personRecordList = personRecordService.queryPersonRecordOrderByNo(name, dept, beginDate, endDate);
			wb = ee.exportResponse(sheetName, columnNumber, columnName, personRecordList);
		}

		long endTime = System.currentTimeMillis(); // 获取结束时间
		long totalTime = startTime - endTime;

		System.out.println(totalTime);
		logger.info("Generation of Excel time :{}", totalTime);

		// 将文件存到浏览器设置的下载位置
		String filename = sheetName + ".xls";
		response.setContentType("application/ms-excel;charset=UTF-8");
		response.setHeader("Content-Disposition",
				"attachment;filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
		OutputStream out = response.getOutputStream();
		try {
			wb.write(out);// 将数据写出去
			String str = "导出" + sheetName + "成功！";
			logger.info("getPersonRecordExcel success :{}", str);
		} catch (Exception e) {
			e.printStackTrace();
			String str1 = "导出" + sheetName + "失败！";
			logger.error("getPersonRecordExcel error:{} , {}", str1, e);
		} finally {
			out.close();
		}
	}

}
